﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Client.XML;
using Client.Common;
using Common;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Supervise();
        }
        private PlanList g_PlanList = null;
        /// <summary>
        /// 监督电脑使用情况
        /// </summary>
        private void Supervise()
        {
            g_PlanList = Helper.GetPlanList();
            FCLogger.WriteInfo("进入监督");
            DayPlan plan = null;
            // 存在今天的计划
            if (Helper.HasPlanedToday(this.g_PlanList, out plan))
            {
                //FCLogger.WriteInfo("存在今天的日志");
                bool isAllowUsePC = false;
                var items = plan.PlanItems.Where(pi => pi.AllowUsePC == "1");
                // 存在今天的允许使用电脑的计划（然后判断现在的时间是否是属于此计划时间段）
                if (items.Count() > 0)
                {
                    List<PlanItem> lst = items.ToList();

                    foreach (var item in lst)
                    {
                        DateTime startDateTime;
                        DateTime endDateTime;
                        if (DateTimeTryParse(item.StartTime, out startDateTime) &&
                            DateTimeTryParse(item.EndTime, out endDateTime))
                        {
                            if (startDateTime.Ticks <= DateTime.Now.Ticks
                                && DateTime.Now.Ticks <= endDateTime.Ticks)
                            {
                                isAllowUsePC = true;
                                //FCLogger.WriteInfo("是在使用电脑期间");
                                break;
                            }
                        }
                    }

                    if (!isAllowUsePC)
                    {
                        string fileName = @"D:\MyWorkspace\教育\FamilyCorperate\Code\Client\bin\Debug\Client.exe 1";
                        //ProcessAsUser.Launch(fileName);
                    }
                }
                // 没有使用电脑计划
                else
                {
                    string fileName = @"D:\MyWorkspace\教育\FamilyCorperate\Code\Client\bin\Debug\Client.exe 1";
                    //ProcessAsUser.Launch(fileName);
                }
            }
            else
            {
                string fileName = @"D:\MyWorkspace\教育\FamilyCorperate\Code\Client\bin\Debug\Client.exe";
                //ProcessAsUser.Launch(fileName);
                //FCLogger.WriteInfo("不存在，设定");
            }
        }

        private bool DateTimeTryParse(string pStrTime, out DateTime pDateTime)
        {
            string strDateTime = string.Format("{0} {1}",
                            DateTime.Now.ToString("yyyy-MM-dd"), pStrTime);
            return DateTime.TryParse(strDateTime, out pDateTime);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Supervise();
        }
    }
}
